#pragma once
#include <data/data_reader.hpp>
#include <vector>


namespace blitz_query_cpp
{
    class memory_data_reader : public data_reader
    {
        int row_num = -1;
        int row_count = 0;
        std::vector<std::string_view> data;

    public:
        memory_data_reader(int column_count_ = 0)
        {
            column_count = column_count_;
        }

        void set_column_count(int column_count_)
        {
            data.clear();
            column_count = column_count_;
            data.reserve(128*column_count);
        }

        void clear()
        {
            column_count = 0;
            data.clear();
        }

        void add_row()
        {
            data.resize(data.size() + column_count);
            row_count++;
        }

        void set_value(int column, std::string_view value)
        {
            if(!row_count)
                return;
            auto value_iter = std::next(data.begin(), (row_count - 1) * column_count + column);
            if(value_iter >= data.end())
                return;
            *value_iter = value;
        }

        virtual bool next_row()
        {
            if(row_num >= row_count)
                return false;
            row_num++;
            return true;
        }

        virtual std::string_view read_value(int column)
        {
            if(row_num < 0)
                return {};
            auto value_iter = std::next(data.begin(), row_num * column_count + column);
            if(value_iter >= data.end())
                return {};
            return *value_iter;
        }
    };
}
